//
//  Ltestview.m
//  view
//
//  Created by 1217 on 15/12/23.
//  Copyright © 2015年 路一枭. All rights reserved.
//

#import "Ltestview.h"
#import "SystemDefine.h"
#import "ZYLBtutton.h"
#import "BillTypeCell.h"





@interface LShowView : UITableView

@end

@implementation LShowView

@end

@interface LCShowView : UICollectionView

@end

@implementation LCShowView

@end




@interface LScrollView : UIScrollView

@end

@implementation LScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.showsHorizontalScrollIndicator = NO;
        self.layer.borderWidth = 0;
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
    } return self;
}

@end





#pragma mark -----------------------------------


@interface Ltestview () <UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
{
    LScrollView *_titleView;
    NSMutableArray *_btnSet;
    LShowView *_openView;
    LCShowView * _cOpenView;
    NSArray *_tableData;
    CGRect rect;
    UIView *_blackView;
    NSInteger _section;
    void (^completion)(NSInteger section,NSInteger row);
}
@end

static NSString *cellId =@"BillTypeCell";
@implementation Ltestview


static CGFloat const rowHeight = 44;//tableView _ __rowHeight
static NSTimeInterval const an_duration = .2f;//动画时间


- (instancetype)initWithFrame:(CGRect)frame delegate:(id<LtestviewDelegate>)delegate
{
    _delegate = delegate;
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        _btnSet = [NSMutableArray array];
        [self addSubview:self.titleView];
        [self addSubview:self.openView];
        [self addSubview:self.cOpenView];
        rect = frame;
        
    } return self;
}


- (void)showInView:(UIView *)view clickAction:(void (^)(NSInteger, NSInteger))block
{
    [view addSubview:self];
    completion = block;
}

- (LScrollView *)titleView
{
    if (!_titleView) {
        _titleView = [[LScrollView alloc]initWithFrame:self.bounds];
    } return _titleView;
}

- (UIView *)blackView
{
    if (!_blackView) {
        _blackView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _blackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.7];
        [_blackView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)]];
        _blackView.alpha = 0;
    } return _blackView;
}

- (void)setTitles:(NSArray *)titles
{
    if (titles.count <1) return;
    _titles = titles;
    NSInteger count = titles.count;
    count = titles.count>3?3:count;
    CGFloat btnWidth = _titleView.frame.size.width/count;
    _titleView.contentSize = (CGSize){btnWidth*titles.count,_titleView.contentSize.height};
    for (UIView *subview in [_titleView subviews]) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [subview removeFromSuperview];
        }
    }
    for (int i =0; i<titles.count; i++) {
        
        CGRect frame = CGRectMake(i*btnWidth, 0, btnWidth, _titleView.frame.size.height);
        NSString *title = titles[i];
        ZYLBtutton *btn = [[ZYLBtutton alloc]initWithFrame:frame];
        btn.tag = i+1;
        btn.titleLabel.numberOfLines =0;
        [btn.titleLabel sizeToFit];
        [btn setTitle:title forState:0];
        [btn setTextAndImageEdgeInsets];
        
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        if (i != titles.count-1 ) {
            [btn addlineWithLineSpacing:10];
        }
        [_titleView addSubview:btn];
    }
}

#pragma mark ----------------------------

- (void)tapAction
{
    if ([_delegate respondsToSelector:@selector(cancelAction)]) {
        [_delegate cancelAction];
    }
    [self click:_btnSet[0]];
}

- (void)click:(UIButton *)sender
{
    if (_btnSet.count >0) {
        if ([_btnSet containsObject:sender]) //返回一个布尔值表明一个给定对象是否存在数组中。
        {
            sender.selected = !sender.selected;
            [self showAnyView:sender];
            return;
        };
        UIButton *btn = _btnSet[0];
        btn.selected = NO;
    }
    sender.selected = !sender.selected;
    _btnSet[0] = sender;
    
    [self showAnyView:sender];
}

- (void)showAnyView:(UIButton *)sender
{
    if (sender.selected) {
        _tableData = _titleDes[_titles[sender.tag-1]];
        if (_tableData.count>9) {
            [self close];
            [self cOpen];
            
        }else{
            [self cClose];
            [self open];
            
        }
        _section = sender.tag;
    } else{
         [self close];
        [self cClose];
    }
    [_openView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
    [_cOpenView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    
}
#pragma ------------
- (void)open
{
    if (!_blackView.superview) {
        [self.superview insertSubview:self.blackView belowSubview:self];
    }
    
    rect.size.height = _tableData.count *rowHeight +_titleView.frame.size.height;
    [self reSetBounds_isOpen:YES];
    
}

- (void)close
{
    rect.size.height = _titleView.frame.size.height;
    [self reSetBounds_isOpen:NO];
}

- (void)reSetBounds_isOpen:(BOOL)open
{
    CGRect t_r = rect;
   // rect.size.height = rect.size.height>[UIScreen mainScreen].bounds.size.height/2?[UIScreen mainScreen].bounds.size.height/2:rect.size.height;
    t_r.origin.y = _titleView.frame.size.height;
    t_r.size.height = rect.size.height -_titleView.frame.size.height;
    [UIView animateWithDuration:an_duration animations:^{
        self.frame = rect;
        _openView.frame = t_r;
        _blackView.alpha = open;
    }];
}

#pragma ------------
- (void)cOpen
{
    if (!_blackView.superview) {
        [self.superview insertSubview:self.blackView belowSubview:self];
    }
    CGFloat  hieght =ceil(_tableData.count/3.f) *rowHeight +_titleView.frame.size.height+20;
    if (hieght >SCREEN_HEIGHT*3/5) {
        hieght =SCREEN_HEIGHT*3/5;
    }
    
    rect.size.height = hieght;
    [self cReSetBounds_isOpen:YES];
    
}

- (void)cClose
{
    rect.size.height = _titleView.frame.size.height;
    [self cReSetBounds_isOpen:NO];
}

- (void)cReSetBounds_isOpen:(BOOL)open
{
    CGRect t_r = rect;
    // rect.size.height = rect.size.height>[UIScreen mainScreen].bounds.size.height/2?[UIScreen mainScreen].bounds.size.height/2:rect.size.height;
    t_r.origin.y = _titleView.frame.size.height;
    t_r.size.height = rect.size.height -_titleView.frame.size.height;
    [UIView animateWithDuration:an_duration animations:^{
        self.frame = rect;
        _cOpenView.frame = t_r;
        _blackView.alpha = open;
    }];
}

#pragma mark ----------------------

- (LShowView *)openView
{
    if (!_openView) {
        _openView = [[LShowView alloc]initWithFrame:(CGRect){0,self.frame.size.height,self.frame.size.width,0} style:UITableViewStylePlain];
        _openView.delegate = self;
        _openView.dataSource = self;
        
    } return _openView;
}
- (LCShowView *)cOpenView
{
    if (!_cOpenView) {
            UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        _cOpenView = [[LCShowView alloc]initWithFrame:(CGRect){0,self.frame.size.height,self.frame.size.width,0} collectionViewLayout:layout];
        _cOpenView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        layout.minimumInteritemSpacing = 5;//cell的最小横向间距
        layout.minimumLineSpacing = 5;//cell的最小纵向间距
            layout.itemSize = CGSizeMake((self.frame.size.width-40)/3.0, 40);//定义每个cell的宽高
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);//每个UICollectionView的上左下右边界
        _cOpenView.delegate = self;
        _cOpenView.dataSource = self;
        [_cOpenView registerNib:[UINib nibWithNibName:@"BillTypeCell" bundle:nil] forCellWithReuseIdentifier:cellId];
    } return _cOpenView;
}
#pragma tableViewDelgate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.font =[UIFont systemFontOfSize:15];
    cell.textLabel.text = _tableData[indexPath.row];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UIButton *btn =(UIButton *)[self viewWithTag:_section];
    [btn setTitle:_tableData[indexPath.row] forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0,-btn.imageView.frame.size.width, 0,btn.imageView.frame.size.width)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width, 0, -btn.titleLabel.frame.size.width)];
    if ([_delegate respondsToSelector:@selector(clickActionInSection:row:row3:)]) {
         UIButton *btn1 =(UIButton *)[self viewWithTag:1];
         UIButton *btn2 =(UIButton *)[self viewWithTag:2];
         UIButton *btn3 =(UIButton *)[self viewWithTag:3];
        [_delegate clickActionInSection:btn2.titleLabel.text row:[NSString stringWithFormat:@"%@",btn1.titleLabel.text] row3:btn3.titleLabel.text];
    }
    if (completion) {
        completion(_section-1,indexPath.row);
    }
    [self click:_btnSet[0]];
}

#pragma mark -- collectionView


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _tableData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BillTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.typeLabel.text =_tableData[indexPath.row];
    return  cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UIButton *btn =(UIButton *)[self viewWithTag:_section];
    [btn setTitle:_tableData[indexPath.row] forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0,-btn.imageView.frame.size.width, 0,btn.imageView.frame.size.width)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width, 0, -btn.titleLabel.frame.size.width)];
    if ([_delegate respondsToSelector:@selector(clickActionInSection:row:row3:)]) {
        UIButton *btn1 =(UIButton *)[self viewWithTag:1];
        UIButton *btn2 =(UIButton *)[self viewWithTag:2];
        UIButton *btn3 =(UIButton *)[self viewWithTag:3];
        [_delegate clickActionInSection:btn2.titleLabel.text row:[NSString stringWithFormat:@"%@",btn1.titleLabel.text] row3:btn3.titleLabel.text];
    }

    if (completion) {
        completion(_section-1,indexPath.row);
    }
    [self click:_btnSet[0]];
}

@end






